import apiClient from "../apiClient";

export enum RecordStatus {
	PENDING = 0, // 审核中
	APPROVED = 1, // 审核通过
	REJECTED = -1, // 审核失败
	ALL = "",
}

export interface RecordQueryParams {
	status?: RecordStatus;
	submitTimeStart?: string;
	submitTimeEnd?: string;
	auditTimeStart?: string;
	auditTimeEnd?: string;
	current?: number;
	pageSize?: number;
}

export const SubmitRecordApi = {
	GET_RECORDS: "/task/records",
	GET_RECORD_INFO: "/task/record_info",
	EXPORT_RECORDS: "/task/exportRecords",
};

export interface Record {
	id: number;
	task_id: number;
	user_id: number;
	createtime: number;
	updatetime: number;
	status: number;
	reviewtime?: number;
	reviewer?: number;
	remark?: string;
	description?: string;
	createtime_text: string;
	status_text: string;
	user: {
		id: number;
		mobile: string;
		avatar: string;
		nickname: string;
		[key: string]: any;
	};
	task: {
		id: number;
		title: string;
		description: string;
		price: string;
		status: number;
		status_text: string;
		[key: string]: any;
	};
	value: Array<{
		id: number;
		task_record_id: number;
		user_id: number | null;
		createtime: number;
		updatetime: number;
		status: number;
		task_field_id: number;
		value: string;
		field: {
			id: number;
			task_id: number;
			field_id: number;
			field_type: string;
			field_label: string;
			required: string;
			placeholder: string;
			sort: number | null;
			createtime: number;
			updatetime: number;
			deletetime: number | null;
			required_text: string;
		};
	}>;
}

const getRecords = (params: RecordQueryParams) =>
	apiClient.get<{ total: number; items: Record[] }>({
		url: SubmitRecordApi.GET_RECORDS,
		params,
	});

const getRecordInfo = (id: string) =>
	apiClient.get<Record>({
		url: SubmitRecordApi.GET_RECORD_INFO,
		params: { id },
	});

const exportRecords = (params: RecordQueryParams) =>
	apiClient.get<{ url: string; filename: string }>({
		url: SubmitRecordApi.EXPORT_RECORDS,
		params,
	});

export default {
	getRecords,
	getRecordInfo,
	exportRecords,
};
